package cn.sjx.modules.nbiot.business.impl;

import cn.sjx.modules.nbiot.business.IGeomagneticUdpBusinessHandler;
import cn.sjx.modules.nbiot.entity.common.AckEnum;
import cn.sjx.modules.nbiot.entity.common.IotProtocolConstants;
import cn.sjx.modules.nbiot.entity.receive.IotProtocolReceiveBase;
import cn.sjx.modules.nbiot.entity.receive.IotProtocolReceive_0x20;
import cn.sjx.modules.nbiot.entity.receive.IotProtocolReceive_0x21;

/**
 * 地磁报文业务处理 接口 实现类
 *
 * <p>
 *  返回业务指令数据为（需判断每条指令的情况，默认返回0x70指令）：
 *      0x70：确认ACK 成功
 *      0x29: 业务处理未完成，通知检查服务器
 *      0x07：收到同一地磁设备的重复进场数据
 *      0x10：收到同一地磁设备的重复离场数据
 *      0x24：地磁设备不存在
 *
 *  需操作业务：
 *      1. 过滤重复入场/离场数据
 *      2. 生成停车订单
 *      3. 记录地磁状态日志
 * </p>
 *
 * Spring容器管理，使用名称需为：geomagneticUdpBusinessHandler
 *
 * @Author: sjx
 * @Date: 2019年09月04日 0004 09:10:16
 */
public abstract class GeomagneticUdpBusinessHandlerImpl implements IGeomagneticUdpBusinessHandler {

    @Override
    public AckEnum handler(IotProtocolReceiveBase receiveBase, String receiveHexDataStr) throws Exception {
        AckEnum ackEnum = null;
        switch (receiveBase.getType()) {
            case IotProtocolConstants.GEOMAGNETIC_RECEIVING_STATE:
                ackEnum = handlerGeomagneticDataMessage((IotProtocolReceive_0x20) receiveBase, receiveHexDataStr);
                break;
            case IotProtocolConstants.GEOMAGNETIC_HEARTBEAT_PACKET:
                ackEnum = handlerGeomagneticHeartbeatPacket((IotProtocolReceive_0x21) receiveBase, receiveHexDataStr);
                break;
            default:
                ackEnum = AckEnum.SUCCESSFUL;
        }
        return ackEnum;
    }

    /**
     * 处理已经接收的地磁数据报文
     *
     * @param receiveBase       接收到的地磁数据实体
     * @param receiveHexDataStr 接收到的原始地磁心跳数据
     * @return
     * @throws Exception
     */
    public abstract AckEnum handlerGeomagneticDataMessage(IotProtocolReceive_0x20 receiveBase, String receiveHexDataStr) throws Exception;

    /**
     * 处理已经接收的地磁心跳数据报文<br/>
     * 需处理
     *
     * @param receiveBase       接收到的地磁心跳数据实体
     * @param receiveHexDataStr 接收到的原始地磁心跳数据
     * @return
     * @throws Exception
     */
    public abstract AckEnum handlerGeomagneticHeartbeatPacket(IotProtocolReceive_0x21 receiveBase, String receiveHexDataStr) throws Exception;

}
